/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package TinTuc;

import java.io.*;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;

/**
 *
 * @author Admin
 */
public class ArticleWriter {

    XMLOutputter printer = null;
    File anXmlDocFile;

    public ArticleWriter() {
        //lets create an XML Printer!
        printer = new XMLOutputter();
    }

    public ArticleWriter(File xmlFile) {
        //lets create an XML Printer!
        printer = new XMLOutputter();
        this.anXmlDocFile = xmlFile;
    }

    public int Count(String cate) {
        SAXBuilder parser = new SAXBuilder();
        try {
            Document doc = parser.build(anXmlDocFile);
            Element domain = doc.getRootElement();
            List<Element> chuyenmuc = domain.getChildren("chuyenmuc");
            Element e = null;
            for (int i = 0; i < chuyenmuc.size(); i++) {
                if (chuyenmuc.get(i).getAttributeValue("name").equals(cate) == true) {
                    return chuyenmuc.get(i).getChildren("page").size();
                }
            }
        } catch (Exception e) {
            return 0;
        }
        return 0;
    }

    public int Count(String cate, int ipage) {
        SAXBuilder parser = new SAXBuilder();
        int dem = 0;
        try {
            Document doc = parser.build(anXmlDocFile);
            Element domain = doc.getRootElement();
            List<Element> chuyenmuc = domain.getChildren("chuyenmuc");
            Element e = null;
            for (int i = 0; i < chuyenmuc.size(); i++) {
                if (chuyenmuc.get(i).getAttributeValue("name").equals(cate) == true) {
                    List<Element> pages = chuyenmuc.get(i).getChildren("page");
                    for (int j = 0; j < pages.size(); j++) {
                        if (Integer.parseInt(pages.get(j).getAttributeValue("number")) == ipage) {
                            e = pages.get(j);
                        }
                    }
                }
            }
            dem = e.getChildren("baibao").size();
        } catch (Exception e) {
        }
        return dem;
    }

    public void Detach(String tenchuyenmuc, int pagenumber) {
        SAXBuilder parser = new SAXBuilder();
        try {
            Document doc = parser.build(anXmlDocFile);

            Element domain = doc.getRootElement();
            List<Element> chuyenmuc = domain.getChildren("chuyenmuc");
            List<Element> pages = null;
            Element e = null;
            for (int i = 0; i < chuyenmuc.size(); i++) {
                if (chuyenmuc.get(i).getAttributeValue("name").equals(tenchuyenmuc) == true) {
                    pages = chuyenmuc.get(i).getChildren("page");
                    for (int j = 0; j < pages.size(); j++) {
                        if (Integer.parseInt(pages.get(j).getAttributeValue("number")) == pagenumber) {
                            e = pages.get(j);
                        }
                    }
                }
            }
            e.detach();
            XMLOutputter xmlOutput = new XMLOutputter();
            BufferedWriter bw = new BufferedWriter(
                    new OutputStreamWriter(
                    new FileOutputStream(this.anXmlDocFile.getCanonicalPath()), "UTF-8"));
            String xmlOut = xmlOutput.outputString(doc);
            bw.write(xmlOut);
            bw.close();
        } catch (Exception ex) {
        }

    }

    public void Detach(String tenchuyenmuc, String link) {
        SAXBuilder parser = new SAXBuilder();
        try {
            Document doc = parser.build(anXmlDocFile);

            Element domain = doc.getRootElement();
            List<Element> chuyenmuc = domain.getChildren("chuyenmuc");
            List<Element> pages = null;

            for (int i = 0; i < chuyenmuc.size(); i++) {
                if (chuyenmuc.get(i).getAttributeValue("name").equals(tenchuyenmuc) == true) { 
                    pages = chuyenmuc.get(i).getChildren("page");
                    for (int j = 0; j < pages.size(); j++) {
                        List<Element> lstBaiBao = pages.get(j).getChildren();
                        for (int k = lstBaiBao.size() - 1; k >= 0; k--) {
                            Element e = lstBaiBao.get(k);
                            String s1 = e.getChildTextTrim("link");
                            if (s1.equals(link)) {
                                e.detach();
                            }
                        }

                    }
                }
            }
            XMLOutputter xmlOutput = new XMLOutputter();
            BufferedWriter bw = new BufferedWriter(
                    new OutputStreamWriter(
                    new FileOutputStream(this.anXmlDocFile.getCanonicalPath()), "UTF-8"));
            String xmlOut = xmlOutput.outputString(doc);
            bw.write(xmlOut);
            bw.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public void IncreasePage(String cate) {
        SAXBuilder parser = new SAXBuilder();
        try {
            Document doc = parser.build(anXmlDocFile);
            Element domain = doc.getRootElement();
            List<Element> chuyenmuc = domain.getChildren("chuyenmuc");
            List<Element> pages = null;
            Element e = null;
            for (int i = 0; i < chuyenmuc.size(); i++) {
                if (chuyenmuc.get(i).getAttributeValue("name").equals(cate)
                        == true) {
                    pages = chuyenmuc.get(i).getChildren("page");
                    for (int j = 0; j < pages.size(); j++) {
                        e = pages.get(j);
                        int k = Integer.parseInt(e.getAttributeValue("number"))
                                + 1;
                        e.setAttribute("number", k + "");
                    }

                }
            }
            XMLOutputter xmlOutput = new XMLOutputter();
            BufferedWriter bw = new BufferedWriter(
                    new OutputStreamWriter(
                    new FileOutputStream(this.anXmlDocFile.getCanonicalPath()), "UTF-8"));
            String xmlOut = xmlOutput.outputString(doc);
            bw.write(xmlOut);
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    public void Write() throws JDOMException, IOException {
        SAXBuilder parser = new SAXBuilder();
        try {

            Element domain = new Element("domain");
            Document doc = new Document(domain);
            XMLOutputter xmlOutput = new XMLOutputter();
            BufferedWriter bw = new BufferedWriter(
                    new OutputStreamWriter(
                    new FileOutputStream(this.anXmlDocFile.getCanonicalPath()), "UTF-8"));
            String xmlOut = xmlOutput.outputString(doc);
            bw.write(xmlOut);
            bw.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Write(String tenchuyenmuc, String url) {
        SAXBuilder parser = new SAXBuilder();
        try {
            Document doc = parser.build(anXmlDocFile);
            Element domain = doc.getRootElement();
            Element e = new Element("chuyenmuc");
            e.setAttribute("name", tenchuyenmuc);
            e.setAttribute("url", url);
            domain.addContent(e);
            XMLOutputter xmlOutput = new XMLOutputter();
            BufferedWriter bw = new BufferedWriter(
                    new OutputStreamWriter(
                    new FileOutputStream(this.anXmlDocFile.getCanonicalPath()), "UTF-8"));
            String xmlOut = xmlOutput.outputString(doc);
            bw.write(xmlOut);
            bw.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Write(String tenchuyenmuc, int pagenumber) {
        SAXBuilder parser = new SAXBuilder();

        try {
            Document doc = parser.build(anXmlDocFile);
            Element domain = doc.getRootElement();
            List<Element> chuyenmuc = domain.getChildren("chuyenmuc");

            Element e = null;
            for (int i = 0; i < chuyenmuc.size(); i++) {
                if (chuyenmuc.get(i).getAttributeValue("name").equals(tenchuyenmuc) == true) {
                    e = chuyenmuc.get(i);
                }
            }
            Element page = new Element("page");
            page.setAttribute("number", pagenumber + "");
            e.addContent(page);
            XMLOutputter xmlOutput = new XMLOutputter();
            BufferedWriter bw = new BufferedWriter(
                    new OutputStreamWriter(
                    new FileOutputStream(this.anXmlDocFile.getCanonicalPath()), "UTF-8"));
            String xmlOut = xmlOutput.outputString(doc);
            bw.write(xmlOut);
            bw.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Write(String tenchuyenmuc, int pagenumber, String link, String title, String img, String date, String summary) {
        SAXBuilder parser = new SAXBuilder();
        try {
            //<editor-fold desc="Neu trang 1 da du 10 bai">
            //if (Count(tenchuyenmuc, pagenumber) >= 10) {
//                IncreasePage(tenchuyenmuc);
//                Write(tenchuyenmuc, 1);
            // }
            //</editor-fold>
            Document doc = parser.build(anXmlDocFile);
            Element domain = doc.getRootElement();
            List<Element> chuyenmuc = domain.getChildren("chuyenmuc");
            Element e = null;
            for (int i = 0; i < chuyenmuc.size(); i++) {
                if (chuyenmuc.get(i).getAttributeValue("name").equals(tenchuyenmuc) == true) {
                    List<Element> pages = chuyenmuc.get(i).getChildren("page");
                    for (int j = 0; j < pages.size(); j++) {
                        if (Integer.parseInt(pages.get(j).getAttributeValue("number")) == pagenumber) {
                            e = pages.get(j);
                        }
                    }
                }
            }
            //<editor-fold desc="bai bao">
            Element baibao = new Element("baibao");
            e.addContent(baibao);

            Element eTemp = new Element("link");
            eTemp.setText(link);
            baibao.addContent(eTemp);

            eTemp = new Element("title");
            eTemp.setText(title);
            baibao.addContent(eTemp);

            eTemp = new Element("img");
            eTemp.setText(img);
            baibao.addContent(eTemp);

            eTemp = new Element("date");
            eTemp.setText(date);
            baibao.addContent(eTemp);

            eTemp = new Element("summary");
            eTemp.setText(summary);
            baibao.addContent(eTemp);

            XMLOutputter xmlOutput = new XMLOutputter();
            BufferedWriter bw = new BufferedWriter(
                    new OutputStreamWriter(
                    new FileOutputStream(this.anXmlDocFile.getCanonicalPath()), "UTF-8"));
            String xmlOut = xmlOutput.outputString(doc);
            bw.write(xmlOut);
            bw.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
